package com.yyk.yykywd.mvvm.model

import com.yyk.yykywd.*
import com.yyk.yykywd.base.BaseResponse
import com.yyk.yykywd.http.ApiService
import com.yyk.yykywd.mvvm.entity.*
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers

/**
 * 文件描述：
 * Created by guo on 2019-11-27.
 */
class HomeModel {
    fun getDepartmentList(depId: String, getHome: GetHome) {
        val newMap = getNewMap()
        putUserId(newMap)
        putDepId(newMap, depId)
        putSign(newMap)
        ApiService.getInstance().api.postListCenter(newMap)
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<HomeBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: HomeBean) {
                        getHome.getInfo(t)
                    }

                    override fun onError(e: Throwable) {
                        getHome.onError(e.toString())
                    }

                })
    }

    fun getSearchList(depId: String, startDate: String, endDate: String, getSearchData: GetSearchData) {
        val newMap = getNewMap()
        putStart(newMap, startDate)
        putOver(newMap, endDate)
        putDepId(newMap, depId)
        putSign(newMap)
        ApiService.getInstance().api.postSearchList(newMap)
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<HomeSearchBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: HomeSearchBean) {
                        getSearchData.getInfo(t)
                    }

                    override fun onError(e: Throwable) {
                        getSearchData.onError(e.toString())
                    }

                })
    }

    fun getReports(id: String, page: String, num: String, getReport: GetReport) {
        val newMap = getNewMap()
        putPage(newMap, page)
        putNum(newMap, num)
        putDepId(newMap, id)
        putSign(newMap)
        ApiService.getInstance().api.postLogList(newMap)
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<DynamicBroadcastBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: DynamicBroadcastBean) {
                        getReport.getInfo(t)
                    }

                    override fun onError(e: Throwable) {
                        getReport.onError(e.toString())
                    }

                })
    }

    interface GetHome : BaseResponse {
        fun getInfo(home: HomeBean)
    }

    interface GetReport : BaseResponse {
        fun getInfo(report: DynamicBroadcastBean)
    }

    interface GetSearchData : BaseResponse {
        fun getInfo(info: HomeSearchBean)
    }
}